package com.felix.algorithms.query;

import java.util.Arrays;

/**
 * BinarySearch
 *
 * @author xjh
 * @version 1.0
 * @date 2019-03-28
 */
public class BinarySearch {

    public static void main(String[] args) {
        Integer[] nums = {5, 78, 123, 98, 1, 3, 6, 0, 1123};
        Arrays.sort(nums);
        Integer key = 78;
        method(nums, key);
    }

    public static Integer method(Integer[] nums, Integer key) {
        int high = nums.length - 1;
        int low = 0;
        while (low < high) {
            int mid = (low + high) >>> 1;
            if (nums[mid] > key) {
                high = mid - 1;
            } else if (nums[mid] < key) {
                low = mid + 1;
            }else{
                return mid;
            }
        }
        return -1;
    }
}
